package com.main.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.main.bean.Task;

@Repository("taskDao")
public class TaskDao {

	@Autowired
	private DataSource dataSource;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	
	/**
	 * 查询任务
	 * @param Task
	 * @return
	 */
	public Task getTaskByNameOrByCornNameOrId(final Task task){
		String sql="select * from QUARTZ_TASK where quartz_name=? or quartz_ano_name=? or quartz_class_path=? or quartz_id=?";
	    jdbcTemplate.query(sql, new Object[]{task.getQuartz_name(),task.getQuartz_ano_name(),task.getQuartz_class_path(),task.getQuartz_id()},new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				                             task.setQuartz_abs(rs.getString("quartz_abs"));
				                             task.setQuartz_ano_name(rs.getString("quartz_ano_name"));;
				                             task.setQuartz_class_path(rs.getString("quartz_class_path"));
				                             task.setQuartz_create_time(new Date(rs.getDate("quartz_create_time").getTime()));
				                             task.setQuartz_corn(rs.getString("quartz_corn"));
				                             task.setQuartz_type(rs.getString("quartz_type"));
				                             task.setQuartz_id(rs.getString("quartz_id"));
				                             task.setQuartz_name(rs.getString("quartz_name"));
				                        }          
		});  
		
		return task;
	}
	
	/**
	 * 查询任务
	 * @param Task
	 * @return
	 */
	public Task getTaskByName(final Task task){
		String sql="select * from QUARTZ_TASK where quartz_name=?";
	    jdbcTemplate.query(sql, new Object[]{task.getQuartz_name()},new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				                             task.setQuartz_abs(rs.getString("quartz_abs"));
				                             task.setQuartz_ano_name(rs.getString("quartz_ano_name"));;
				                             task.setQuartz_class_path(rs.getString("quartz_class_path"));
				                             task.setQuartz_create_time(new Date(rs.getDate("quartz_create_time").getTime()));
				                             task.setQuartz_corn(rs.getString("quartz_corn"));
				                             task.setQuartz_type(rs.getString("quartz_type"));
				                             task.setQuartz_id(rs.getString("quartz_id"));
				                        }          
		});  
		
		return task;
	}
	
	/**
	 * 查询任务
	 * @param Task
	 * @return
	 */
	public Task getTaskByCornName(final Task task){
		String sql="select * from QUARTZ_TASK where quartz_ano_name=?";
	    jdbcTemplate.query(sql, new Object[]{task.getQuartz_ano_name()},new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				                             task.setQuartz_abs(rs.getString("quartz_abs"));
				                             task.setQuartz_name(rs.getString("quartz_name"));;
				                             task.setQuartz_class_path(rs.getString("quartz_class_path"));
				                             task.setQuartz_create_time(new Date(rs.getDate("quartz_create_time").getTime()));
				                             task.setQuartz_corn(rs.getString("quartz_corn"));
				                             task.setQuartz_type(rs.getString("quartz_type"));
				                             task.setQuartz_id(rs.getString("quartz_id"));
				                        }          
		});  
		
		return task;
	}
	
	/**
	 * 查询是否存在任务
	 * @param Task
	 * @return
	 */
	public int getTaskCount(Task task){
		String sql="select count(*) from QUARTZ_TASK where quartz_name=? or quartz_ano_anme=? or quartz_class_path=? or quartz_id=?";
		Integer result=jdbcTemplate.queryForObject(sql,new Object[]{task.getQuartz_name(),task.getQuartz_ano_name(),task.getQuartz_class_path(),task.getQuartz_id()},Integer.class);
		return result.intValue();
	}
	
	/**
	 * 添加任务
	 */
	public int add(Task task){
		String sql="insert into QUARTZ_TASK(QUARTZ_ID,QUARTZ_NAME,QUARTZ_ANO_NAME,QUARTZ_CLASS_PATH,QUARTZ_CORN,QUARTZ_ABS) values(QUARTZ_TASK_SEQ.nextval,?,?,?,?,?)";
		return jdbcTemplate.update(sql, new Object[]{task.getQuartz_name(),task.getQuartz_ano_name(),task.getQuartz_class_path(),task.getQuartz_corn(),task.getQuartz_abs()});
	}
	
	/**
	 * 获取任务的类名子
	 * @return
	 */
	public List<Map<String,Object>>getAllInitTaskName(){
		String sql="select QUARTZ_NAME from QUARTZ_TASK where QUARTZ_TYPE='0'";
		return jdbcTemplate.queryForList(sql);
	}
	
	/**
	 * 获取所有执行的任务
	 * @return
	 */
	public List<Task>getAllTask(){
		String sql="select * from QUARTZ_TASK";
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(Task.class));
	}
	
	/**
	 * 获取所有分页任务
	 * @return
	 */
	public List<Task>getPageAllTask(String sql){
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(Task.class));
	}
	
	/**
	 * 更新任务数据
	 * @param task
	 * @return
	 */
	public int updateTask(Task task){
		String sql="update QUARTZ_TASK set quartz_name=?,quartz_ano_name=?,quartz_class_path=?,quartz_corn=?,quartz_abs=? where quartz_id=?";
		return jdbcTemplate.update(sql, new Object[]{task.getQuartz_name(),task.getQuartz_ano_name(),task.getQuartz_class_path(),task.getQuartz_corn(),task.getQuartz_abs(),task.getQuartz_id()});
	}
	
	/**
	 * 更新任务数据
	 * @param task
	 * @return
	 */
	public int delTask(Task task){
		String sql="update QUARTZ_TASK set quartz_type='0' where quartz_id=?";
		return jdbcTemplate.update(sql, new Object[]{task.getQuartz_id()});
	}
	
	/**
	 * 更新任务数据
	 * @param task
	 * @return
	 */
	public int updateTaskaddExcute(Task task){
		String sql="update QUARTZ_TASK set quartz_type='1' where quartz_id=?";
		return jdbcTemplate.update(sql, new Object[]{task.getQuartz_id()});
	}
	
	/**
	 * 更新任务数据
	 * @param task
	 * @return
	 */
	public int updateTaskPauseExcute(Task task){
		String sql="update QUARTZ_TASK set quartz_type='2' where quartz_id=?";
		return jdbcTemplate.update(sql, new Object[]{task.getQuartz_id()});
	}
	
	/**
	 * 分页获取页数 
	 */
	public String getCountPage(String sql){
		String result=jdbcTemplate.queryForObject(sql,String.class);
		return result;
	}
	
}
